//
//  IMListViewController.m
//  TQY
//
//  Created by 甘世清 on 2024/10/18.
//

#import "IMListViewController.h"
#import "IMListTableViewCell.h"
#import "IMListModel.h"
#import "HomeLetterViewController.h"
@interface IMListViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation IMListViewController
- (void)SQ_viewWillAppear:(BOOL)animated{
    [self SQ_Header];
}
- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.zx_navTitle = SQNSLocalString(@"用户消息");
    WS(weakSelf);
    [self zx_setRightBtnWithText:SQNSLocalString(@"通知") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) {
        HomeLetterViewController * vc = [[HomeLetterViewController alloc]init];
        [weakSelf SQ_pushViewControllerFull:vc];
    }];
  
}

- (void)SQ_UIViewForSelfView{
    UIImageView * bgImageView = [[UIImageView alloc]init];
    bgImageView.image = SQGetImage(@"二级背景");
    bgImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.view addSubview:bgImageView];
    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.top.equalTo(self.zx_navBar.mas_bottom);
    }];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(SQ_Header) name:@"IMListModel" object:nil];
}
- (void)SQ_Header{
    [self.jsonArray removeAllObjects];
    [self.jsonArray addObjectsFromArray:[TQYFMDBManager SQ_IMAcquire]];
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
     return self.jsonArray.count;
}
 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    IMListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"IMListTableViewCell"];
    if (!cell) {
        cell = [[IMListTableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"IMListTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = [self.jsonArray objectAtIndex:indexPath.item];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 84;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    IMMessageViewController * vc = [[IMMessageViewController alloc]init];
    vc.listModel = [self.jsonArray objectAtIndex:indexPath.item];
    [self SQ_pushViewControllerFull:vc];
    
}

- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath{
    WS(weakSelf);
    UIContextualAction * delete = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:SQNSLocalString(@"删除") handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        completionHandler(YES);
        [weakSelf SQ_deleted:indexPath];
    }];
    delete.backgroundColor = UIColor.redColor;
    UISwipeActionsConfiguration * config = [UISwipeActionsConfiguration configurationWithActions:@[delete]];
    return config;
}
-(void)SQ_deleted:(NSIndexPath*)indexPath{
  IMListModel *  model = [self.jsonArray objectAtIndex:indexPath.item];
   BOOL isok = [TQYFMDBManager SQ_deletet:model.roomId];
    if (isok) {
        [self.jsonArray removeObject:model];
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}



@end
